package com.hsingh.eclipse.mercury.extension.launchconfiguration;

import java.util.ArrayList;
import java.util.List;
import java.util.Set;

import org.eclipse.core.expressions.IEvaluationContext;
import org.eclipse.core.runtime.CoreException;
import org.eclipse.debug.core.DebugPlugin;
import org.eclipse.debug.core.ILaunchConfiguration;
import org.eclipse.debug.core.ILaunchConfigurationType;
import org.eclipse.debug.core.ILaunchManager;

import com.hsingh.eclipse.mercury.extensionpoint.definition.ITask;
import com.hsingh.eclipse.mercury.extensionpoint.definition.ITaskProvider;

public class LaunchConfigurationTaskProvider implements ITaskProvider {

  public LaunchConfigurationTaskProvider() {}

  @Override
  public List<ITask> getTasks(IEvaluationContext context) {
    ArrayList<ITask> tasks = new ArrayList<ITask>();

    ILaunchManager mgr = DebugPlugin.getDefault().getLaunchManager();
    try {
      for (ILaunchConfiguration l : mgr.getLaunchConfigurations()) {
        ILaunchConfigurationType type = l.getType();
        for (Object o : type.getSupportedModeCombinations()) {
          if (o instanceof Set) {
            @SuppressWarnings("unchecked")
            Set<String> set = (Set<String>) o;
            for (String mode : set) {
              tasks.add(new LaunchConfigurationTask(l, mode));
            }
          }
        }
      }
    } catch (CoreException e) {
      e.printStackTrace();
    }

    return tasks;
  }

}
